package cn.new7as.cas5s.ui.login;


import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import androidx.annotation.NonNull;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.OnAdidReadListener;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.ui.slideshow.MainActivity;
import cn.new7as.cas5s.R;
import cn.new7as.cas5s.tool.util.SharePreferenceUtils;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.databinding.ActivityPwdloginBinding;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.LoginInApi;
import cn.new7as.cas5s.ui.easy.netapi.LoginInPwdApi;


public class PwdLoginActivity extends BaseActivity {


    private ActivityPwdloginBinding binding;

    private boolean isshoweye;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPwdloginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initV();

    }

    String akj;
    String akjda;

    public void initV() {
        Adjust.getGoogleAdId(this, s -> akj = s);
        Adjust.getAdid(new OnAdidReadListener() {
            @Override
            public void onAdidRead(String s) {
                akjda = s + "";
            }
        });
        binding.forgetpwd.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        String data = getIntent().getStringExtra("data");


        binding.backsds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });


        binding.forgetpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.go(PwdLoginActivity.this, ForgetPwdActivity.class);


            }
        });
        binding.loginVeri.setOnClickListener(v -> {
            Utils.goextra(PwdLoginActivity.this, VeriedActivity.class, data);
            finish();
        });
        binding.show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isshoweye) {
                    binding.pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    binding.show.setBackgroundResource(R.mipmap.eye_close);
                } else {
                    binding.pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    binding.show.setBackgroundResource(R.mipmap.eye_open);
                }

                isshoweye = !isshoweye;
            }
        });
        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = binding.pwd.getText().toString();
                if (pwd.isEmpty())
                    toa(getString(R.string.pwd_empty));
                else
                    loginIn(data, pwd);
            }
        });
    }

    void loginIn(String pho, String ver) {
        show();
        EasyHttp.post(this)
                .api(new LoginInPwdApi(pho, ver, akj, akjda)).request(new OnHttpListener<HttpData<LoginInApi.LoginData>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<LoginInApi.LoginData> result) {
                        hide();
                        SharePreferenceUtils spUtils = new SharePreferenceUtils(PwdLoginActivity.this);
                        spUtils.putString("useruniquecode", result.getResult().token);
                        spUtils.putString("phonenum", result.getResult().mobile);
                        spUtils.putString("ucode", result.getResult().userCode);
                        Utils.go(PwdLoginActivity.this, MainActivity.class);
                        finish();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }


}